package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.BaseJdbcDao;
import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.global.util.StringUtils;
import com.bringway.p2p.dao.CodeDao;
import net.sf.json.JSONObject;

/**
 * @author js
 * @version 创建时间：2016年6月30日 上午11:28
 * @功能：公共代码界面
 */
@Repository
public class CodeDaoImpl extends BaseJdbcDao implements CodeDao {
	Logger logger =Logger.getLogger(CodeDaoImpl.class);
	
	/**
	 * @author js
	 * @功能：增加节点
	 */
	@Override
	public int addMenu(JSONObject parms) {
		String name = parms.optString("name");
		String code_alias = parms.optString("code_alias");
		String code_value = parms.optString("code_value");
		String comment = parms.optString("comment");
		String order_index = parms.optString("order_index");
		int parent_id = parms.optInt("parent_id", 0);
		StringBuffer sql=new StringBuffer();
		sql.append("INSERT INTO t_fm_sys_common_code (NAME,CODE_ALIAS,CODE_VALUE,PARENT_ID,CODE_COMMENT,ORDER_INDEX) ");
		sql.append("  VALUES (:NAME,:CODE_ALIAS,:CODE_VALUE,:PARENT_ID,:CODE_COMMENT,:ORDER_INDEX)");
		SqlParameterSource parm = new MapSqlParameterSource()
				.addValue("NAME", name)
				.addValue("CODE_ALIAS", code_alias)
				.addValue("CODE_VALUE", code_value)
				.addValue("PARENT_ID", parent_id)
				.addValue("CODE_COMMENT", comment)
				.addValue("ORDER_INDEX", order_index);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parm);
	}
	/**
	 * @author js
	 * @功能：删除节点
	 */
	@Override
	public int deleteMenu(JSONObject parms) {
		int id = parms.optInt("id");
		String sql = "DELETE FROM t_fm_sys_common_code WHERE ID= :id OR PARENT_ID=:id";
		SqlParameterSource paramMap = new MapSqlParameterSource().addValue("id", id);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

	/**
	 * @author js
	 * @功能：修改节点
	 */
	@Override
	public int updateMenu(JSONObject parms) {
		int id = parms.optInt("id");
		String name = parms.optString("name");
		String alias = parms.optString("alias");
		String value = parms.optString("value");
		String index = parms.optString("index");
		String comment = parms.optString("comment");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		sql.append(" UPDATE t_fm_sys_common_code SET");
		sql.append(" name = :name,code_alias=:alias,code_value = :value,order_index = :index");
		sql.append(",code_comment = :comment WHERE id = :id  ");

			parmsMap.put("id", id);
			parmsMap.put("name", name);
			parmsMap.put("alias", alias);
			parmsMap.put("value", value);
			parmsMap.put("index", index);
			parmsMap.put("comment", comment);
			
		logger.info("updateMenu sql is "+sql.toString());
		logger.info("updateMenu parmsMap is "+parmsMap);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parmsMap);
	}
	/**
	 * @author js
	 * @功能：查询树节点
	 */
	@Override
	public List<Map<String, Object>> queryMenuListWithOutCheckBox(JSONObject params) {
		String sql = " select t1.ID,t1.NAME,t1.CODE_ALIAS,t1.CODE_VALUE,t1.CODE_COMMENT,t1.ORDER_INDEX,t1.PARENT_ID,case when 1=1 then (select COUNT(*) from t_fm_sys_common_code t2 where t2.PARENT_ID=t1.ID) end CHILDCT from t_fm_sys_common_code t1 where PARENT_ID = :nodeId ORDER BY CHILDCT DESC,id asc ";
		return this.getNamedParameterJdbcTemplate().queryForList(sql, params);
	}
	@Override
	public int getQueryMenuCount(JSONObject params) {
		int parent_id = params.optInt("nodeId", 0);
		String name = params.optString("name");
		String alias = params.optString("alias");
		String comment = params.optString("comment");
		
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT count(*) FROM t_fm_sys_common_code t1 ");
		sql.append(" LEFT join t_fm_sys_common_code t2 on T1.PARENT_ID=T2.ID WHERE T1.PARENT_ID=:nodeId");
		if (StringUtils.isNotEmpty(name)) {
			sql.append(" and T1.name like '%"+name+"%'");
		}
		if (StringUtils.isNotEmpty(alias)) {
			sql.append(" and T1.code_alias like '%"+alias+"%'");
		}
		if (StringUtils.isNotEmpty(comment)) {
			sql.append(" and T1.code_comment like '%"+comment+"%'");
		}
		Map<String, Object> paramMap=new HashMap<>();
		paramMap.put("nodeId", parent_id);
		logger.info("getQueryMenuCount sql is "+sql.toString());
		logger.info("getQueryMenuCount paramMap is "+paramMap);
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}
	@Override
	public List<Map<String, Object>> queryMenuList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		int parent_id = params.optInt("nodeId", 0);
		String name = params.optString("name");
		String alias = params.optString("alias");
		String comment = params.optString("comment");
		
		StringBuffer sql=new StringBuffer();
		//sql.append("SELECT * FROM t_fm_sys_common_code where ID in ");
		sql.append("SELECT T1.ID, T1.NAME, T1.CODE_ALIAS, T1.CODE_VALUE, T1.PARENT_ID, T1.CODE_COMMENT, T1.ORDER_INDEX,t2.NAME PARENT_NAME FROM t_fm_sys_common_code t1");
		sql.append(" LEFT JOIN t_fm_sys_common_code T2 ON T1.PARENT_ID=T2.ID WHERE T1.PARENT_ID=:nodeId ");
		if (StringUtils.isNotEmpty(name)) {
			sql.append(" and T1.name like '%"+name+"%'");
		}
		if (StringUtils.isNotEmpty(alias)) {
			sql.append(" and T1.code_alias like '%"+alias+"%'");
		}
		if (StringUtils.isNotEmpty(comment)) {
			sql.append(" and T1.code_comment like '%"+comment+"%'");
		}
		sql.append(" order by T1.id desc limit :start,:limit");
		Map<String, Object> paramMap=new HashMap<>();
		paramMap.put("nodeId", parent_id);
		paramMap.put("start", start);
		paramMap.put("limit", limit);
		logger.info("queryMenuList sql is "+sql.toString());
		logger.info("queryMenuList paramMap is "+paramMap);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}

}
